package com.sunrise.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sunrise.util.RestTemplateUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liansen
 * @Date: 2024-05-05  15:56
 */
@RestController
@RequestMapping("/kaoqin")
public class KaoQinController {

    @Resource
    private RestTemplateUtil restTemplateUtil;

    /**
     * 判断当前日期是否是节假日
     * @return
     */
    @GetMapping("/test")
    public Object test() {
        //LocalDate now = LocalDate.now();
        String url = "http://timor.tech/api/holiday/info/" + "2024-04-28";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        //模拟真实请求
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        return restTemplateUtil.get(url, headers, String.class);
    }

    /**
     * 返回2024全年节假日信息
     * @return 返回全年节假日信息
     */
    @GetMapping("/test2")
    public Object test2() {
        String url = "http://timor.tech/api/holiday/year/2024?type=Y&week=Y";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        //模拟真实请求
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        ResponseEntity<String> responseEntity = restTemplateUtil.get(url, headers, String.class);
        String body = responseEntity.getBody();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        return jsonObject;
    }
}
